import time
from functools import cmp_to_key

class Solution(object):
    def largestNumber(self, nums):
        def compare(a, b):
            if str(a) + str(b) > str(b) + str(a):
                return -1
            elif str(a) + str(b) < str(b) + str(a):
                return 1
            else:
                return 0
        
        nums = sorted(nums, key=cmp_to_key(compare))
        result = ''.join(map(str, nums))
        return result if result[0] != '0' else '0'


def test_cases():
    """基础测试用例"""
    solution = Solution()
    
    tests = [
        ([10, 2], "210"),
        ([3, 30, 34, 5, 9], "9534330"),
        ([1], "1"),
        ([0, 0], "0"),
        ([12, 121], "12121"),
        ([432, 43], "43432"),
    ]
    
    print("测试结果")
    print("=" * 40)
    
    for i, (nums, expected) in enumerate(tests, 1):
        result = solution.largestNumber(nums)
        status = "✅" if result == expected else "❌"
        
        print(f"测试 {i}: {nums}")
        print(f"期望: {expected}")
        print(f"结果: {result} {status}\n")


def performance_test():
    """性能测试"""
    solution = Solution()
    
    print("性能测试")
    print("=" * 20)
    
    for n in [10, 50, 100, 500]:
        nums = list(range(1, n + 1))
        
        start_time = time.time()
        result = solution.largestNumber(nums)
        end_time = time.time()
        
        print(f"n={n}: {(end_time - start_time)*1000:.2f} ms")


if __name__ == "__main__":
    test_cases()
    performance_test()
